/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmViewEstabelecimentos.java
 *
 * Created on 11/02/2010, 15:30:23
 */

package forms;

import classes.contratos;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import rad.Java.Common.DB.Database;
import rad.Java.Common.DB.Search;

/**
 *
 * @author rogerio
 */
public class frmViewEstabelecimentos extends javax.swing.JFrame {

    private contratos listContratos = new contratos();
    String fDia  = "yyyy";
    SimpleDateFormat sdfD = new SimpleDateFormat(fDia);
    Date d = new Date();
    Search busca = new Search();

    /** Creates new form frmViewEstabelecimentos */
    public frmViewEstabelecimentos() {
        initComponents();
        fillComboBoxContratos();
        fDia = sdfD.format(d);
        cmbAno1.setSelectedItem(Integer.parseInt(fDia));
        //cmbAno2.setSelectedItem(Integer.parseInt(fDia));
        cmbAno1.setEnabled(false);
        //cmbAno2.setEnabled(false);
    }


    // <editor-fold defaultstate="collapsed" desc="Functons">
    private void fillComboBoxContratos()
    {
        for(int i = 1990; i < 2020; i++)
        {
            cmbAno1.addItem(i);
            //cmbAno2.addItem(i);
        }
    }

    private void viewRelatorios()
    {
        String query;
        ResultSet rs;
        try {
            HashMap parametros = new HashMap();
            if(rbnTodos.isSelected())
            {
                query = "SELECT DISTINCT ESTABELECIMENTOS.estab_name AS ESTABELECIMENTOS_estab_name,"+
                        "ESTABELECIMENTOS.estab_telefone AS ESTABELECIMENTOS_estab_telefone,"+
                        "ESTABELECIMENTOS.estab_cnpjCpf AS ESTABELECIMENTOS_estab_cnpjCpf "+
                        "FROM ESTABELECIMENTOS ESTABELECIMENTOS";
                        rs = Database.executeQueryRs( query );
            }
            else
            {
                busca.prepare();
                //busca.setDistinct(true);
                String value = "ESTABELECIMENTOS.estab_name AS ESTABELECIMENTOS_estab_name,"+
                        "ESTABELECIMENTOS.estab_telefone AS ESTABELECIMENTOS_estab_telefone,"+
                        "ESTABELECIMENTOS.estab_cnpjCpf AS ESTABELECIMENTOS_estab_cnpjCpf ";

                busca.setFields(value);
                busca.setTable("estabelecimentos");
                //busca.setTable("contratos", "contratos.estabelecimentos_id = estabelecimentos.id");

                int ano1 = Integer.parseInt(cmbAno1.getSelectedItem().toString());
                /*int ano2 = Integer.parseInt(cmbAno2.getSelectedItem().toString());
                int aux;

                if(ano2 < ano1)
                {
                    aux  = ano1;
                    ano1 = ano2;
                    ano2 = aux;
                }*/

               //busca.setConditionalWhere("ano < "+ano1);
                busca.setConditionalWhere("id NOT IN (SELECT estabelecimentos_id FROM contratos WHERE ano = "+ano1+")");
                query = busca.getSql();
                rs = Database.executeQueryRs( query );
            }
            JRResultSetDataSource jrRS = new JRResultSetDataSource( rs );
            JasperPrint jasperPrint = JasperFillManager.fillReport("reports/estabelecimentos.jasper", parametros, jrRS);
            JasperViewer.viewReport(jasperPrint, false);
        }
        // </editor-fold>
        catch (JRException ex) {
            Logger.getLogger(frmViewEstabelecimentos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // </editor-fold>

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        cmbAno1 = new javax.swing.JComboBox();
        rbnTodos = new javax.swing.JRadioButton();
        rbnData = new javax.swing.JRadioButton();
        btnPesquisar = new javax.swing.JButton();
        btnDeletar1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Relatório Estabelecimentos");

        cmbAno1.setName("cmbAno1"); // NOI18N

        buttonGroup1.add(rbnTodos);
        rbnTodos.setSelected(true);
        rbnTodos.setText("Todos");
        rbnTodos.setName("rbnTodos"); // NOI18N
        rbnTodos.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rbnTodosStateChanged(evt);
            }
        });

        buttonGroup1.add(rbnData);
        rbnData.setText("Data");
        rbnData.setName("rbnData"); // NOI18N
        rbnData.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rbnDataStateChanged(evt);
            }
        });

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/search_32x32.png"))); // NOI18N
        btnPesquisar.setToolTipText("Pesquisar");
        btnPesquisar.setName("btnPesquisar"); // NOI18N
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnDeletar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/forms/resources/exit_32x32.png"))); // NOI18N
        btnDeletar1.setToolTipText("Sair");
        btnDeletar1.setName("btnDeletar1"); // NOI18N
        btnDeletar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbnTodos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbnData)
                        .addGap(14, 14, 14)
                        .addComponent(cmbAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnPesquisar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeletar1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbnTodos)
                    .addComponent(rbnData)
                    .addComponent(cmbAno1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnDeletar1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnDeletar1, btnPesquisar});

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-237)/2, (screenSize.height-151)/2, 237, 151);
    }// </editor-fold>//GEN-END:initComponents

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        viewRelatorios();
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void rbnDataStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rbnDataStateChanged
       if(rbnData.isSelected())
       {
           cmbAno1.setEnabled(true);
           //cmbAno2.setEnabled(true);
       }
    }//GEN-LAST:event_rbnDataStateChanged

    private void rbnTodosStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rbnTodosStateChanged
       if(rbnTodos.isSelected())
       {
           cmbAno1.setEnabled(false);
           //cmbAno2.setEnabled(false);
       }
    }//GEN-LAST:event_rbnTodosStateChanged

    private void btnDeletar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletar1ActionPerformed
        this.dispose();
}//GEN-LAST:event_btnDeletar1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmViewEstabelecimentos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeletar1;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmbAno1;
    private javax.swing.JRadioButton rbnData;
    private javax.swing.JRadioButton rbnTodos;
    // End of variables declaration//GEN-END:variables

}
